package com.example.studentalarm.adapter;

import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.example.studentalarm.utils.LogUtil;

public class CustomViewPagerAdapter<V extends View> extends PagerAdapter {

    private V[] views;

    public CustomViewPagerAdapter(V[] views) {
        super();
        this.views = views;
    }

    @Override
    public void finishUpdate(View arg0) {
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public Object instantiateItem(ViewGroup arg0, int arg1) {
        arg0.addView(views[arg1 % views.length], 0);
        return views[arg1 % views.length];
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == (arg1);
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void destroyItem(ViewGroup arg0, int arg1, Object arg2) {
        LogUtil.d("Views.length ==" + views.length + " == childCount ==" + ((ViewPager) arg0).getChildCount());
        arg0.removeView(views[arg1 % views.length]);
    }

    @Override
    public void startUpdate(View arg0) {

    }

    public V[] getAllItems() {
        return views;
    }
}